﻿Public Class HandAlarmForm


    Private _netid As Byte

    '地址
    Private _offset As UShort = 7032

    Private Sub HandAlarmForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _netid = DevTypeService.GetMinDevId(MainForm.pi.ProjectID)
        SetModifyState(False)
        Me.btnModify.Enabled = False
        Timer1.Start() 'LoadData()
#If LSC_PROJECTTYPE_SWITCH = "SALES" Then
    me.btnSave.Visible = False 
#End If
    End Sub

    ''' <summary>
    ''' 读取当前手动状态设定值
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadData()
        Dim buf() As Short


        'buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordWritableRead, _offset, 1)
        buf = COMHelper.Instance.Send4Short(_netid, FunctionCode.WordWritableRead, _offset, 1)
        If buf Is Nothing OrElse buf.Length <> 1 Then
            Me.btnModify.Enabled = False
            Exit Sub
        End If

        Me.btnModify.Enabled = True

        If buf(0) = 0 Then
            Me.RadioButton1.Checked = True
        Else
            Me.RadioButton2.Checked = True
        End If
    End Sub


    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    ''' <summary>
    ''' 开始修改
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        Me.btnModify.Enabled = False

        SetModifyState(True)
    End Sub

    Private Sub SetModifyState(ByVal value As Boolean)
        Me.btnModify.Enabled = Not value
        Me.btnSave.Enabled = value

        Me.GroupBox1.Enabled = value

    End Sub

    ''' <summary>
    ''' 保存修改
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor

        result = Save()

        Me.Cursor = Cursors.Default
        If result Then
            SetModifyState(False)
            msg.Show("保存成功")
        Else
            msg.ShowError("保存失败")
        End If

    End Sub


    ''' <summary>
    ''' 保存 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        Dim value As Short
        If Me.RadioButton1.Checked Then
            value = 0
        Else
            value = 1
        End If

        'Return COMHelper.Instance.Write(g_netID, FunctionCode.WordWrite, _offset, value)
        Return COMHelper.Instance.Write(_netid, FunctionCode.WordWrite, _offset, value)
    End Function

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        LoadData()
    End Sub
End Class